package com.joe.xl.common.mybatisplus.generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

public class CodeGenerator {
    private static String GENERATOR_MODULE_DIR = "/joe-common/joe-common-framework";
    private static String GENERATOR_MODULE_PARENT = "com.joe.xl.common.framework";
    private static String TABLE_NAME = "crm_customer";

    public static void main(String[] args) {
        String projectDir = System.getProperty("user.dir"); // joemaster
        System.out.println(projectDir);

        AutoGenerator generator = new AutoGenerator();

        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectDir + GENERATOR_MODULE_DIR + "/src/main/java");
        gc.setAuthor("joe");
        gc.setOpen(false);
        generator.setGlobalConfig(gc);

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:sqlserver://192.168.60.27:1433;databaseName=BICRM;");
        dataSourceConfig.setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        dataSourceConfig.setUsername("sa");
        dataSourceConfig.setPassword("cc.123");
        generator.setDataSource(dataSourceConfig);

        PackageConfig pc = new PackageConfig();
//        pc.setModuleName(GENERATOR_MODULE_DIR);
        pc.setParent(GENERATOR_MODULE_PARENT);
        generator.setPackageInfo(pc);

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setInclude(TABLE_NAME);
        generator.setStrategy(strategyConfig);

        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        generator.execute();
    }
}
